mod common;
use crate::common::*;
use crate::goods::{ck_goods, goods_data};
use base::base::VMap;
use serde_json::json;

/*  1. no any data
 *  2. add one goods
 *  3. get all goods
 *  4. get the added goods item
 *  5. rm the goods item
 *  6. get all goods
 *  7. get the removed goods item
 *  8. rm the goods item again
 */
#[tokio::test]
async fn test_goods_1() {
    let val = goods_data();

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "",
                "name": "name_te1",
            }
        }),
        "goods",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "name": "name_te1",
            }
        }),
        "goods",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        e_node(),
    )
    .await;

    ck_goods(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "goods",
        "add",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year_1").unwrap(),
            "te1": val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        json!([val.ck_val("te1").unwrap()]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "get",
        val.ck_val("te1").unwrap().clone(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "rm",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "get",
        e_node(),
    )
    .await;

    ck_goods(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "rm",
    )
    .await;

    ck_goods(pos!(), json!({})).await;
}

/*  1. add two goods
 *  2. get all goods
 *  3. modify one goods
 *  4. get all goods
 *  5. get modified goods
 *  6. remove the modified goods, can't be remove
 *  7. modify one goods
 *  8. remove the modified goods
 *  9. remove the other goods
 *  10. get all goods
 */
#[tokio::test]
async fn test_goods_2() {
    let val = goods_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "goods",
        "add",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year_1").unwrap(),
            "te1": val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te2",
                "name": "name_te2",
            }
        }),
        "goods",
        "add",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year").unwrap(),
            "te1": val.ck_val("te1").unwrap(),
            "te2": val.ck_val("te2").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        json!([val.ck_val("te1").unwrap(), val.ck_val("te2").unwrap(),]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": [{
                "sn": "te1",
                "name": "n_te1",
                "task": 1,
            }]
        }),
        "goods",
        "md",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        json!([val.ck_val("c_te1").unwrap(), val.ck_val("te2").unwrap(),]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "get",
        val.ck_val("c_te1").unwrap().clone(),
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year").unwrap(),
            "te1": val.ck_val("c_te1").unwrap(),
            "te2": val.ck_val("te2").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "rm",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year_2").unwrap(),
            "te2": val.ck_val("te2").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te2",
        }),
        "goods",
        "rm",
    )
    .await;

    ck_goods(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_goods_6() {
    let val = goods_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "goods",
        "add",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year_1").unwrap(),
            "te1": val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te2",
                "name": "name_te2",
            }
        }),
        "goods",
        "add",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year").unwrap(),
            "te1": val.ck_val("te1").unwrap(),
            "te2": val.ck_val("te2").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "ex": ["index"],
            "v": [
            {"sn": "te1", "name": "n_te1", "index": 1, "task": 1},
            {"sn": "te2", "name": "n_te2", "index": 2},
            ]
        }),
        "goods",
        "md",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year").unwrap(),
            "te1": val.ck_val("c_te1").unwrap(),
            "te2": val.ck_val("c_te2").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "goods",
        "list",
        json!([val.ck_val("c_te1").unwrap(), val.ck_val("c_te2").unwrap()]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "goods",
        "rm",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "all": val.ck_val("all").unwrap(),
            "year": val.ck_val("year_2").unwrap(),
            "te2": val.ck_val("c_te2").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te2",
        }),
        "goods",
        "rm",
    )
    .await;

    ck_goods(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_goods_7() {
    let val = goods_data();

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1"
        }),
        "goods",
        "listpsn",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
            "v": [{
                "sn": "psn1",
                "name": "name_psn1",
            }]
        }),
        "goods",
        "addpsn",
    )
    .await;

    ck_goods(
        pos!(),
        json!({
            "psn": val.ck_val("psn").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1"
        }),
        "goods",
        "listpsn",
        json!([val.ck_val("c_psn1").unwrap()]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        json!({
            "sn": "psn1",
            "name": "name_psn1",
            "task": "te1",
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "goods",
        "rmpsn",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "te1"
        }),
        "goods",
        "listpsn",
        e_node(),
    )
    .await;
}
